


from flask import Blueprint
from flask import request
from flask import jsonify
import psycopg2.extras
from database import db_pool_singleton
from database.user_info_table_method import UserInfoTableMethod
from database.user_operation_log_table_method import UserOperationLogTableMethod

operation_log_blueprint = Blueprint('operation_log', __name__)

'''-- 用户操作日志表    
CREATE TABLE IF NOT EXISTS user_operation_log (    
    log_id SERIAL PRIMARY KEY,    
    user_id INTEGER NOT NULL REFERENCES user_info(user_id),    
    operation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,    
    operation_type VARCHAR(20) NOT NULL,    
    operation_content VARCHAR(200) NOT NULL    
);'''

@operation_log_blueprint.route('/insert_user_operation_log', methods=['POST'])
def insert_user_operation_log():

    user_id = request.form.get('user_id')

    operation_type = request.form.get('operation_type')

    operation_content = request.form.get('operation_content')

    if not user_id or not operation_type or not operation_content:

        args_error = {
            "status": -1,
            "errorCode": 4001,
            "errorMsg": "args error",
            "resultBody": {}
        }
        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:

        cursor= conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_operation_log_table_method = UserOperationLogTableMethod(conn=conn, cursor=cursor)

        result = user_operation_log_table_method.insert_user_operation_log(
            user_id=user_id, 
            operation_type=operation_type, 
            operation_content=operation_content
        )

        if not result:
            args_error = {
                "status": -1,
                "errorCode": 4002,
                "errorMsg": "insert error",
                "resultBody": {}
            }
            return jsonify(args_error)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": "insert success"
        }

        return jsonify(result_body)


@operation_log_blueprint.route('/get_user_operation_log_by_user_id', methods=['POST'])
def get_user_operation_log_by_user_id():


    user_id = request.form.get('user_id')

    limit = request.form.get('limit', 10)

    page = request.form.get('page', 1)


    if not user_id:

        args_error = {
            "status": -1,
            "errorCode": 4003,
            "errorMsg": "args error",
            "resultBody": {}

        }

        return jsonify(args_error)
    

    with db_pool_singleton.getconn() as conn:

        cursor= conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_operation_log_table_method = UserOperationLogTableMethod(conn=conn, cursor=cursor)

        result = user_operation_log_table_method.get_user_operation_log_by_user_id(user_id=user_id, limit=limit, page=page)

        if not result:
            args_error = {
                "status": -1,
                "errorCode": 4004,
                "errorMsg": "get error",
                "resultBody": {}
            }
            return jsonify(args_error)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": result
        }
        return jsonify(result_body)
    

@operation_log_blueprint.route('/get_user_operation_log_by_user_id_and_operation_type', methods=['POST'])
def get_user_operation_log_by_user_id_and_operation_type():

    user_id = request.form.get('user_id')

    operation_type = request.form.get('operation_type')

    limit = request.form.get('limit', 10)

    page = request.form.get('page', 1)


    if not user_id or not operation_type:

        args_error = {
            "status": -1,
            "errorCode": 4005,
            "errorMsg": "args error",
            "resultBody": {}

        }

        return jsonify(args_error)
    

    with db_pool_singleton.getconn() as conn:

        cursor= conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_operation_log_table_method = UserOperationLogTableMethod(conn=conn, cursor=cursor)

        result = user_operation_log_table_method.get_user_operation_log_by_user_id_and_operation_type(user_id=user_id, operation_type=operation_type, limit=limit, page=page)


        if not result:
            args_error = {
                "status": -1,
                "errorCode": 4006,
                "errorMsg": "get error",
                "resultBody": {}
            }
            return jsonify(args_error)

        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": result
        }
        return jsonify(result_body)
    
@operation_log_blueprint.route('/get_all_user_operation_log')
def get_all_user_operation_log():


    with db_pool_singleton.getconn() as conn:


        cursor= conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_operation_log_table_method = UserOperationLogTableMethod(conn=conn, cursor=cursor)


        result = user_operation_log_table_method.get_all_user_operation_log()

        if not result:
            args_error = {
                "status": -1,
                "errorCode": 4007,
                "errorMsg": "get error",
                "resultBody": {}
            }
            return jsonify(args_error)
        
        result_body = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": result
        }
        return jsonify(result_body)


    


